﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Ledger
{
    public partial class LedgerLineDialog : Form
    {
        private List<Transaction> _transactions = new List<Transaction>();

        public LedgerLine Info
        {
            get
            {
                LedgerLine l = new LedgerLine();
                l.Budget = Decimal.Parse(textAmount.Text);
                l.Description = textDescription.Text;
                l.Transactions = this._transactions;
                return l;
            }
            set
            {
                this._transactions = value.Transactions;
                textAmount.Text = value.Budget.ToString();
                textDescription.Text = value.Description;
            }
        }

        private void Populate()
        {
            decimal total = 0.0m;
            listTransactions.Items.Clear();
            foreach (Transaction t in this._transactions)
            {
                ListViewItem lvi = new ListViewItem(t.Description);
                lvi.SubItems.Add(t.Amount.ToString("C"));
                if (t.IsRefund)
                    lvi.SubItems.Add("Yes");
                else
                {
                    lvi.SubItems.Add("No");
                }
                listTransactions.Items.Add(lvi);
                if (t.IsRefund)
                    total -= t.Amount;
                else
                    total += t.Amount;
            }
            labelBudget.Text = "To Date: " + total.ToString("C");
        }

        public LedgerLineDialog()
        {
            InitializeComponent();
        }

        private void toolCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void toolAdd_Click(object sender, EventArgs e)
        {
            TransactionDialog dlg = new TransactionDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this._transactions.Add(dlg.Info);
                Populate();
            }
        }

        private void toolEdit_Click(object sender, EventArgs e)
        {
            int index = listTransactions.SelectedIndices[0];
            if (index >= 0)
            {
                TransactionDialog dlg = new TransactionDialog();
                dlg.Info = this._transactions[index];
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this._transactions[index] = dlg.Info;
                    Populate();
                }
            }
        }

        private void toolOK_Click(object sender, EventArgs e)
        {
            bool ok = true;

            if (textDescription.Text == "")
            {
                ok = false;
                MessageBox.Show("Please enter a description for this line.");
            }

            Regex r = new Regex(Properties.Resources.currencyRegex);
            if (!r.IsMatch(textAmount.Text))
            {
                ok = false;
                MessageBox.Show("Please enter a budget amount for this line.");
            }

            if (ok)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
